package com.abing.starry.service.impl;

import com.abing.starry.common.ErrorCode;
import com.abing.starry.exception.ThrowUtils;
import com.abing.starry.model.request.todo.TodoQueryRequest;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.abing.starry.model.entity.TodoEntity;
import com.abing.starry.service.TodoService;
import com.abing.starry.mapper.TodoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


/**
* @author 阿炳亿点点帅
* @description 针对表【todo】的数据库操作Service实现
* @createDate 2024-08-11 20:22:08
*/
@Service
public class TodoServiceImpl extends ServiceImpl<TodoMapper, TodoEntity>
    implements TodoService{

    @Override
    public IPage<TodoEntity> queryTodo(TodoQueryRequest todoQueryRequest) {
        Page<TodoEntity> page = new Page<>(todoQueryRequest.getCurrent(), todoQueryRequest.getPageSize());
        Page<TodoEntity> todoEntityPage = this.page(page, Wrappers.lambdaQuery(TodoEntity.class)
                                                                  .eq(TodoEntity::getUserId, todoQueryRequest.getUserId()));
        return todoEntityPage;
    }

    @Override
    public TodoEntity queryTodoById(String id) {
        ThrowUtils.throwIf(StringUtils.isEmpty(id), ErrorCode.PARAMS_ERROR);
        return this.getById(id);
    }

    @Override
    public boolean createTodo(TodoEntity todoEntity) {
        return this.save(todoEntity);
    }

    @Override
    public boolean deleteTodo(String id) {
        ThrowUtils.throwIf(StringUtils.isEmpty(id),ErrorCode.PARAMS_ERROR);
        return this.removeById(id);
    }

    @Override
    public boolean updateTodo(TodoEntity todoEntity) {
        ThrowUtils.throwIf(StringUtils.isEmpty(todoEntity.getId()),ErrorCode.PARAMS_ERROR);
        return this.updateById(todoEntity);
    }
}




